package {
	import away3d.containers.View3D;
	import away3d.debug.AwayStats;
	import away3d.library.AssetLibrary;
	import away3d.lights.DirectionalLight;
	import away3d.lights.PointLight;
	import away3d.loaders.parsers.Max3DSParser;
	import away3d.materials.ColorMaterial;
	import away3d.materials.lightpickers.LightPickerBase;
	import away3d.materials.lightpickers.StaticLightPicker;
	import away3d.materials.methods.EffectMethodBase;
	import away3d.materials.methods.EnvMapMethod;
	import away3d.textures.BitmapCubeTexture;
	import controllers.OrbitController;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.setInterval;
	import object.cars.AstonMartinVintage;
	import object.cars.AudiS3;
	import object.cars.CarBase;
	import object.cars.ChevroletCamaro;
	import object.cars.FerrariCalifornia;
	import object.cars.MercedesMCLarenSLR500;
	import object.cars.MitsubishiLancerX;
	import object.cars.Nissan350z;
	import object.environments.Garage;
	
	/**
	 * 主场景
	 * @author yungzhu@gmail.com
	 * Morn UI：flash轻量级，高性能，可视化UI解决方案 https://code.google.com/p/morn/
	 */
	public class Main extends Sprite {
		//车库环境贴图
		[Embed(source="assets/garage/EnvPosXGarage.jpg")]
		private var EnvPosXGarage:Class;
		[Embed(source="assets/garage/EnvPosYGarage.jpg")]
		private var EnvPosYGarage:Class;
		[Embed(source="assets/garage/EnvPosZGarage.jpg")]
		private var EnvPosZGarage:Class;
		[Embed(source="assets/garage/EnvNegXGarage.jpg")]
		private var EnvNegXGarage:Class;
		[Embed(source="assets/garage/EnvNegYGarage.jpg")]
		private var EnvNegYGarage:Class;
		[Embed(source="assets/garage/EnvNegZGarage.jpg")]
		private var EnvNegZGarage:Class;
		
		private var _view:View3D;
		private var _orbitController:OrbitController;
		private var _car:CarBase;
		
		private var _bodyMaterial:ColorMaterial;
		private var _glassMaterial:ColorMaterial;
		private var _bumperMaterial:ColorMaterial;
		private var _wheelMaterial:ColorMaterial;
		private var _cameraLight:PointLight;
		
		public function Main():void {
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.frameRate = 60;
			stage.addEventListener(Event.ENTER_FRAME, onStageEnterFrame);
			stage.addEventListener(Event.RESIZE, onStageResize);
			init3D();
			initText();
		}
		
		private function init3D():void {
			_view = new View3D();
			_view.antiAlias = 4;
			addChild(_view);
			//统计
			addChild(new AwayStats(_view));
			//仅允许解析3ds格式的模型文件
			AssetLibrary.enableParser(Max3DSParser);
			//手动视角控制器
			_orbitController = new OrbitController(_view.camera, _view);
			_orbitController.activate = true;
			//光源
			var light:DirectionalLight = new DirectionalLight(0, -1, 0);
			light.color = 0xEEEEEE;
			_cameraLight = new PointLight();
			_cameraLight.color = 0x777777;
			var lightPicker:StaticLightPicker = new StaticLightPicker([light, _cameraLight]);
			_view.scene.addChild(light);
			_view.scene.addChild(_cameraLight);
			//实例车库
			var garage:Garage = new Garage(light);
			_view.scene.addChild(garage);
			//车库环境贴图
			var cubeTextures:BitmapCubeTexture = new BitmapCubeTexture(new EnvPosXGarage().bitmapData, new EnvNegXGarage().bitmapData, new EnvPosYGarage().bitmapData, new EnvNegYGarage().bitmapData, new EnvPosZGarage().bitmapData, new EnvNegZGarage().bitmapData);
			//环境贴图模式,让车身可以有镜面效果
			_bodyMaterial = createColorMaterial(0x242424, 1, 1, lightPicker, new EnvMapMethod(cubeTextures, 0.3));
			_glassMaterial = createColorMaterial(0xFFFFFF, 0.4, 1, lightPicker, new EnvMapMethod(cubeTextures));
			_bumperMaterial = createColorMaterial(0x444444, 1, 0.1, lightPicker, new EnvMapMethod(cubeTextures, 0.5));
			_wheelMaterial = createColorMaterial(0xEEEEEE, 1, 0.5, lightPicker, new EnvMapMethod(cubeTextures, 0.5));
			//实例车
			randomCar();
			//右键切换车型
			stage.addEventListener(MouseEvent.RIGHT_CLICK, onClick);
			//随机车体颜色
			setInterval(randomColor, 3000);
			//增加提示
		}
		
		private function randomColor():void {
			_bodyMaterial.color = Math.random() * 0xffffff;
		}
		
		private function onClick(e:MouseEvent):void {
			randomCar();
		}
		
		private function randomCar():void {
			if (_car && _car.parent) {
				_view.scene.removeChild(_car);
			}
			var a:Array = [AstonMartinVintage, AudiS3, ChevroletCamaro, FerrariCalifornia, MercedesMCLarenSLR500, MitsubishiLancerX, Nissan350z];
			_car = new a[int(a.length * Math.random())](_bodyMaterial, _bumperMaterial, _glassMaterial, _wheelMaterial);
			_view.scene.addChild(_car);
		}
		
		private function createColorMaterial(color:uint, alpha:Number, specular:Number, lightPicker:LightPickerBase, method:EffectMethodBase):ColorMaterial {
			var mater:ColorMaterial = new ColorMaterial(color, alpha);
			mater.specular = specular;
			mater.lightPicker = lightPicker;
			mater.addMethod(method);
			return mater;
		}
		
		private function onStageResize(e:Event):void {
			_view.width = stage.stageWidth;
			_view.height = stage.stageHeight;
		}
		
		private function onStageEnterFrame(e:Event):void {
			_cameraLight.position = _view.camera.position;
			_view.render();
			_orbitController.update();
		}
		
		private function initText():void {
			var text:TextField = new TextField();
			text.defaultTextFormat = new TextFormat("Verdana", 11, 0xFFFFFF);
			text.width = 240;
			text.height = 100;
			text.selectable = false;
			text.mouseEnabled = false;
			text.text = "右键点击切换车型";
			text.filters = [new DropShadowFilter(1, 45, 0x0, 1, 0, 0)];
			text.x = 150;
			text.y = 2;
			addChild(text);
		}
	}
}